package com.bluishoul.bean;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

import my.db.QueryHelper;
import my.util.MyStringUtils;

import org.apache.commons.lang.StringUtils;

@SuppressWarnings("serial")
public class Shop extends Pojo {
	private long user;
	private String name;
	private String pinyin;
	private String tel;
	private String addr_prv;
	private String addr_city;
	private String addr_district;
	private String addr_street;
	private String website;
	private String description;
	private int deliver_speed;
	private String img;
	private int status;
	private Timestamp create_time;

	public Shop hasSame() {
		String sql = "SELECT * FROM menu_shop WHERE  name=? && addr_prv=? and addr_city=? && addr_district=?";
		return QueryHelper.read(Shop.class, sql, name, addr_prv, addr_city,
				addr_district);
	}

	public boolean Update() {
		String sql = "UPDATE menu_shop SET pinyin=?,tel=?,addr_prv=?,addr_city=?,addr_district=?,addr_street=?,website=?,description=?,img WHERE id=?";
		pinyin = MyStringUtils.getPinyin("name");
		return Evict(QueryHelper.update(sql, name, pinyin, tel, addr_prv,
				addr_city, addr_district, addr_street, website, description,
				img, getId()) == 1);
	}

	public List<Long> listAllShops(int page, int count,int status, String... pinyin) {
		String key = "SL#" + page + "#" + count;
		StringBuilder sb = new StringBuilder("SELECT * FROM menu_shop ");
		List<String> pylist = Arrays.asList(pinyin);
		if (pylist.size() > 0)
			for (String py : pylist) {
				if (pylist.indexOf(py) == 0)
					sb.append(" WHERE ");
				if(py.length()>1)
					continue;
				sb.append(" pinyin LIKE '");
				sb.append(py);
				sb.append("%'");
				if (pylist.indexOf(py) != pylist.size() - 1)
					sb.append(" OR");
				else
					sb.append(" AND status>?");
			}
		else
			sb.append("WHERE status=?");
			return QueryHelper.query_slice_cache(Long.class, CACHE_1_HOUR, key,
					200, sb.toString(), page, count,status);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getAddr_prv() {
		return addr_prv;
	}

	public void setAddr_prv(String addr_prv) {
		this.addr_prv = addr_prv;
	}

	public String getAddr_city() {
		return addr_city;
	}

	public void setAddr_city(String addr_city) {
		this.addr_city = addr_city;
	}

	public String getAddr_street() {
		return addr_street;
	}

	public void setAddr_street(String addr_street) {
		this.addr_street = addr_street;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Timestamp getCreate_time() {
		return create_time;
	}

	public void setCreate_time(Timestamp create_time) {
		this.create_time = create_time;
	}

	public String getAddr_district() {
		return addr_district;
	}

	public void setAddr_district(String addr_district) {
		this.addr_district = addr_district;
	}

	@Override
	protected String TableName() {
		return "menu_shop";
	}

	@Override
	protected boolean IsObjectCachedByID() {
		return true;
	}

	public int getDeliver_speed() {
		return deliver_speed;
	}

	public void setDeliver_speed(int deliver_speed) {
		this.deliver_speed = deliver_speed;
	}

	public String getPinyin() {
		if (StringUtils.isBlank(pinyin) && StringUtils.isNotBlank(name))
			pinyin = MyStringUtils.getPinyin(name);
		return pinyin;
	}

	public void setPinyin(String pinyin) {
		this.pinyin = pinyin;
	}

	public String getImg() {
		return img;
	}

	public void setImg(String img) {
		this.img = img;
	}

	@Override
	public long Save() {
		return super.Save();
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public long getUser() {
		return user;
	}

	public void setUser(long user) {
		this.user = user;
	}
}
